//
//  ViewController.m
//  testFFMpeg2
//
//  Created by Met on 2021/8/7.
//

#import "ViewController.h"
#import "avcodec.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
    const char* c = avcodec_configuration();
    NSString* s = [[NSString alloc] initWithCString:c encoding:NSUTF8StringEncoding];
    NSLog(@"con=%@",s);
}

- (IBAction)startCmd:(id)sender {
    
    [self runCmd];
}

- (void)runCmd{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"ffmpeg" ofType:@""];

    NSString *ret = [self runCommand:path];
    
    NSLog(@"cmd ret:%@",ret);
}

// Run a command
-(NSString*)runCommand:(NSString*)cmd {
// Create an empty string
    NSMutableString* mutableString = [NSMutableString stringWithCapacity:0];

// Create a new task
    NSTask *task = [[NSTask alloc] init];
    
    NSString *ts = @"/Users/billanderson/Desktop/down51/FFMPEGNew/ffmpeg-build-scripts/build/mac-x86_64/ffmpeg/bin/test.ts";
    NSString *m4 = @"/Users/billanderson/Desktop/down51/FFMPEGNew/ffmpeg-build-scripts/build/mac-x86_64/ffmpeg/bin/test3.mp4";
    
    
//   转码命令
//    ffmpeg -i test.ts -acodec copy -vcodec copy -f mp4 test.mp4
// Configure the task
    
//    task.arguments = @[ts];
    task.arguments = @[@"-i",ts,@"-acodec",@"copy",@"-vcodec",@"copy",@"-f",@"mp4",m4];
    
    [task setStandardOutput: [NSPipe pipe]];
    [task setStandardError: [task standardOutput]];
    [task setLaunchPath:cmd];

    // Launch the task
    [task launch];
    // Append the output
    NSData *data;
    while ((data = [[[task standardOutput] fileHandleForReading] availableData]) && [data length]) {
        NSString* output = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] ;
        [mutableString appendString:output];
    }
    // Make sure the task is finished
    [task terminate];
    // Release the thread pool

    // Return the output
    return mutableString;

}

@end
